(function (pageName) {
  // body...
  sugarcane.module[pageName] = function(){
    $('.j-pagescreen').addClass('fn-hide');
    var urlParams = sugarcane.getUrlParams(),
      type = urlParams['docType'],
      id = urlParams['id'],
      detailData = {};;

    if ($('#j-screen-'+pageName+id).length) {
      var pageScreen = $('#j-screen-'+pageName+id);
      pageScreen.removeClass('fn-hide');
      var _scrollTop = pageScreen.data('scrolltop') || 0;
      setTimeout(function(){
        $(window).scrollTop(_scrollTop);
      }, 10);
      return false;
    }
    var pageScreen = $(`<div class="j-pagescreen j-pagescreen-detail app-poster" id="j-screen-`+pageName+id+`">
      </div>`);
    $('body').append(pageScreen);

    detailShow();
    function detailShow(){
      if(type == '3'){//快电
        var _obj = $(`
          <div><img class="fastimg j-img" src="" alt="" /></div>
          <div class="content j-content"></div>
          <div class="newsfooter"><img src="/static/img/posternewsfoot.png"" alt="" /></div>
          `);
        pageScreen.append(_obj);
        sugarcane.ajaxApi({
          "url": "/news/poster",
          "data": {"id": id,"docType": type},
          "callback": function(res){
            if(res['code'] == '0'){
              //detailData = res['data'];
              _obj.find('.j-img').attr('src',res['data'].imgUrl);
              pageScreen.find('.j-content').html(res['data'].content);
            }
          }
        });
      }else{//新闻
        var _obj = $(`
          <div class="head"><img src="/static/img/posterhead.png"" alt="" /></div>
          <div class="j-content"></div>
          <div class="newsfooter"><img src="/static/img/posternewsfoot.png"" alt="" /></div>
          `);
        pageScreen.append(_obj);
        sugarcane.ajaxApi({
          "url": "/news/poster",
          "data": {"id": id,"docType": type},
          "callback": function(res){
            if(res['code'] == '0'){
              detailData = res['data'];
               // 标题
              pageScreen.find('.j-content').append(newGrapes["newstitle"].init({"title":detailData['title']}));
              // 发布时间 
          
              pageScreen.find('.j-content').append(newGrapes["newstime"].init({"sub1":detailData['source'],"sub2":" "+detailData['author']+ " ","sub3":"发布时间："+detailData['publishTime'],"subright1":"","subright2":"","subright3":""}));

              // 内容
              if (detailData['content']) {
                var voiceContent = '';
                var newscontentObj = newGrapes["newscontent"].init({"content":detailData['content']});

                // 处理编辑携带的字体大小 
                // newscontentObj.find('p').css("font-size", "inherit").css('line-height','inherit');
                // newscontentObj.find('*').css("font-size", "inherit").css('line-height',"inherit");

                // 处理编辑携带的字体大小，按屏幕大小等比显示，还有行高
                newscontentObj.find('*').each(function(){
                  var _fontSize = this.style.fontSize,
                    _lineHeight = this.style.lineHeight;
                  if (_fontSize) {
                    _fontSize = _fontSize.toLowerCase();
                    if (_fontSize.indexOf('px') > -1) {
                      _fontSize = (_fontSize.replace('px', '') * pageScreen.width() / 375) + 'px';
                      $(this).css('font-size', _fontSize);
                    }
                  }
                  if (_lineHeight) {
                    _lineHeight = _lineHeight.toLowerCase();
                    if (_lineHeight.indexOf('px') > -1) {
                      _lineHeight = (_lineHeight.replace('px', '') * pageScreen.width() / 375) + 'px';
                      $(this).css('line-height', _lineHeight);
                    }
                  }
                });
                // 处理图片大小
                newscontentObj.find('img').each(function(){
                  var _this = this;
                  //图标图片和正文图片
                  var _width = $(_this).attr('width'),
                    _height = $(_this).attr('height');
                  if (_width) {
                    _width = _width.replace('px', '') * pageScreen.width() / 375;
                    $(_this).attr('width', _width);
                  }
                  if (_height) {
                    _height = _height.replace('px', '') * pageScreen.width() / 375;
                    $(_this).attr('height', _height);
                  }
                  if(!_width && !_height){
                    //如果图片没有任何样式，以像素来显示
                    var newImage = new Image();
                    newImage.src = $(this).attr('src');
                    if (newImage.complete) { 
                      var _width = newImage.width,
                        _height = newImage.height;
                      _width = _width * pageScreen.width() / 375;
                      $(_this).css('width', _width);
                      $(_this).css('max-width', '100%').css('display','block');
                    }else{
                      newImage.onload = function(){
                        var _width = this.width,
                          _height = this.height;
                        _width = _width * pageScreen.width() / 375;
                        $(_this).css('width', _width);
                        $(_this).css('max-width', '100%').css('display','block');
                      }
                    }
                    
                  }
                    
                });

                // 设置字体大小
                pageScreen.addClass('app-fontsize-' + sugarcane['app']['fontSize']);

                // 无图模式
                if (sugarcane['app']['appNopic']) {
                  newscontentObj.find('img').remove();
                }

                pageScreen.find('.j-content').append(newscontentObj);
                voiceContent = newscontentObj.find('.news-content').text();//语音播放的内容

                if (!sugarcane['app']['isShare']) {
                  formatContent(newscontentObj);
                }
                // 责编和编辑
                if (detailData['editor'] || detailData['responsibleEditor']) {
                  var editorObj = $('<div></div>');
                  if (detailData['editor'] && detailData['responsibleEditor']) {
                    editorObj.html('<p style="text-align:right">（编辑:'+ detailData['editor'] +'  责编:'+ detailData['responsibleEditor'] +'）</p>');
                  } else if (detailData['editor']) {
                    editorObj.html('<p style="text-align:right">（编辑:'+ detailData['editor'] +'）</p>');
                  } else if (detailData['responsibleEditor']) {
                    editorObj.html('<p style="text-align:right">（责编:'+ detailData['responsibleEditor'] +'）</p>');
                  }
                  newscontentObj.find('.news-content').append(editorObj);
                }

              }
            }
          }
        });
      }
    }
    
  }
  sugarcane.module['state'][pageName] = 'load';
  sugarcane.module.callback(pageName);
}('poster'));